package com.zlp.quartz.first;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.concurrent.TimeUnit;

/**
 * @Author: zhulaiping
 * @Date: 2021/10/16 17:19
 */
public class QuartzTest {

    /*
    * 验证jop可以被两个triger触发
    * */
    public static void main(String[] args) {

        try {
            //创建调度器
            Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
            scheduler.start();
            //创建job：name用来标记任务的，group用来标记任务和方便管理的
            JobDetail job = JobBuilder.newJob(HelloJob.class)
                    .withIdentity("job1", "group1")
                    .build();
            //创建触发器
            Trigger trigger1 = TriggerBuilder.newTrigger()
                    .withIdentity("triger1", "group1")
                    .startNow()
                    .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                            .withIntervalInSeconds(4)
                            .repeatForever())
                    .build();

            Trigger trigger2 = TriggerBuilder.newTrigger()
                    .withIdentity("triger2", "group1")
                    .forJob("job1","group1")
                    .startNow()
                    .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                            .withIntervalInSeconds(4)
                            .repeatForever())
                    .build();

            //4.调度
            scheduler.scheduleJob(job,trigger1);
            scheduler.scheduleJob(trigger2);

            TimeUnit.SECONDS.sleep(10);
            scheduler.shutdown();
        } catch (SchedulerException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}
